import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/task/main/models/plan_task.dart';
import 'package:leader_help/utils/component/dialog/bottom.dart';
import 'package:leader_help/utils/date_format/date_util.dart';
import 'package:leader_help/utils/ifly/ifly_util.dart';
import 'package:leader_help/utils/ifly/xf_api_utils.dart';

import 'index.dart';

class PlanTaskController extends GetxController {
  PlanTaskController();

  final state = PlanTaskState();

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.commentCtrl.addListener(() {
      state.mod?.comment = state.commentCtrl.text;
    });
    state.departmentList = [
      '河南省人民政府办公厅',
      '河南省财政厅',
      '河南省工业和信息化厅',
      '河南省商务厅',
      '河南省农业农村厅',
      '河南省发展和改革委员会',
      '河南省教育厅',
      '河南省科学技术厅',
    ];
    state.mod = PlanTaskModel();
  }

  void actionSure() {
    if (state.mod?.mainDepartment == null) {
      EasyLoading.showToast('请选择牵头部门');
      return;
    }
    if (state.mod?.aidDepartment == null) {
      EasyLoading.showToast('请选择协同部门');
      return;
    }
    if (state.mod?.comment == null) {
      EasyLoading.showToast('请语音或者手动录入您对此任务的重点批示');
      return;
    }
    state.mod?.date =
        DateFormat.format(DateTime.now(), DateFormatType.ymd, isUnit: true);
    Get.back(result: state.mod);
  }

  void actionMain() {
    showActionSheet(
      titles: state.departmentList,
      title: '部门列表',
      choosedIndex:
          state.departmentList.indexOf(state.mod?.mainDepartment ?? ''),
      onTap: (p0) {
        state.mod?.mainDepartment = state.departmentList[p0];
        update();
      },
    );
  }

  void actionAid() {
    showActionSheet(
      titles: state.departmentList,
      title: '部门列表',
      choosedIndex:
          state.departmentList.indexOf(state.mod?.aidDepartment ?? ''),
      onTap: (p0) {
        state.mod?.aidDepartment = state.departmentList[p0];
        update();
      },
    );
  }

  void actionAudio() async {
    ifly_audio(textCtrl: state.commentCtrl);
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
